package utpl.edu.ec;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class MainActivity extends Activity
{
    private static final String NAMESPACE = "http://servicios.eva.org/";
    private static String URL = "http://172.16.17.210:8080/serviciosEva/evaWS?WSDL";
    private static final String METHOD_NAME = "login_eva";
    private static final String SOAP_ACTION = "http://servicios.eva.org/evaWS/login_evaRequest";
    
    /** Called when the activity is first created. */
    
    EditText txtUsuario;
    EditText txtPass;
    Button btnLogin;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setContentView(R.layout.main);

        txtUsuario = (EditText) findViewById(R.id.txtUsuario);
        txtPass = (EditText) findViewById(R.id.txtPass);

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("usuario", txtUsuario.getText().toString());
                request.addProperty("pasword", txtPass.getText().toString());
                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
                soapEnvelope.dotNet = false;
                soapEnvelope.setOutputSoapObject(request);
                HttpTransportSE htrans = new HttpTransportSE(URL);

                try {
                    htrans.call(SOAP_ACTION, soapEnvelope);

                    SoapPrimitive result = (SoapPrimitive) soapEnvelope.getResponse();
                    if (Boolean.parseBoolean(result.toString())) {
                        Toast.makeText(getApplicationContext(), R.string.bienvenida , Toast.LENGTH_LONG).show();
                        Intent iPeriodos = new Intent(MainActivity.this, PeriodosActivity.class);
                        iPeriodos.putExtra("cedula", "1104635519");
                        startActivity(iPeriodos);
                    } else {
                        Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_LONG).show();
                    }


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}
